package com.webside.points.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webside.points.mapper.AnswerMapper;
import com.webside.points.model.AnswerEntity;
import com.webside.points.service.AnswerService;
import com.webside.points.wechat.Util;
@Service
public class AnswerServiceImpl implements AnswerService{

	@Autowired
	AnswerMapper answerMapper;
	
	@Override
	public int insert(AnswerEntity answerEntity) {
		//base64加密
		answerEntity.setAnswerContent(Util.getBase64(answerEntity.getAnswerContent()));
		
		return answerMapper.insert(answerEntity);
	}

	@Override
	public List<AnswerEntity> findByExpert(int expertId) {
		List<AnswerEntity> list = answerMapper.findByExpert(expertId);
		if(list!=null){
			for(int i=0;i<list.size();i++){
				AnswerEntity answer = list.get(i);
				//解密base64
				answer.setAnswerContent(Util.getFromBase64(answer.getAnswerContent()));
				list.set(i, answer);
			}
		}
		
		return list;
	}

	@Override
	public int addAnswer(AnswerEntity answerEntity) {
		return answerMapper.addAnswer(answerEntity);
	}

	@Override
	public int updateLikeMark(AnswerEntity answerEntity) {
		return answerMapper.updateLikeMark(answerEntity);
	}

	@Override
	public AnswerEntity findById(int answerId) {
		AnswerEntity answerEntity=answerMapper.findById(answerId);
		if(answerEntity!=null){
			answerEntity.setAnswerContent(Util.getFromBase64(answerEntity.getAnswerContent()));
		}
		
		return answerEntity;
	}

	@Override
	public int delete(int answerId) {
		return answerMapper.delete(answerId);
	}

	@Override
	public int updateAddAnswer(int answerId) {
		return answerMapper.updateAddAnswer(answerId);
	}

	@Override
	public List<AnswerEntity> findByIssueId(Map<String,Integer> map) {
		List<AnswerEntity> list = answerMapper.findByIssueId(map);
		if(list!=null){
			for(int i=0;i<list.size();i++){
				AnswerEntity answer = list.get(i);
				//解密base64
				answer.setAnswerContent(Util.getFromBase64(answer.getAnswerContent()));
				list.set(i, answer);
			}
		}
		
		return list;
	}

	@Override
	public int updateStatus(AnswerEntity answerEntity) {
		return answerMapper.updateStatus(answerEntity);
	}

}
